<template>
  <div class="table-box">
    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="检验单号" prop="inspectionNo">
        <el-input v-model="searchForm.inspectionNo" placeholder="请输入检验单号" clearable />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="searchForm.productName" placeholder="请输入产品名称" clearable />
      </el-form-item>
      <el-form-item label="检验类型" prop="inspectionType">
        <el-select v-model="searchForm.inspectionType" placeholder="请选择检验类型" clearable style="width: 180px">
          <el-option label="首件检验" value="FIRST_PIECE" />
          <el-option label="过程检验" value="PROCESS" />
          <el-option label="最终检验" value="FINAL" />
          <el-option label="出货检验" value="OUTGOING" />
        </el-select>
      </el-form-item>
      <el-form-item label="检验结果" prop="result">
        <el-select v-model="searchForm.result" placeholder="请选择检验结果" clearable style="width: 180px">
          <el-option label="合格" value="PASS" />
          <el-option label="不合格" value="FAIL" />
          <el-option label="待检验" value="PENDING" />
        </el-select>
      </el-form-item>
      <el-form-item label="检验时间" prop="dateRange">
        <el-date-picker
          v-model="searchForm.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增检验</el-button>
      <el-button type="success" :icon="Download" @click="handleExport">导出</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="inspectionNo" label="检验单号" />
      <el-table-column prop="inspectionType" label="检验类型">
        <template #default="scope">
          <el-tag :type="getTypeTagType(scope.row.inspectionType)">{{ getTypeText(scope.row.inspectionType) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="productCode" label="产品编码" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="batchNo" label="批次号" />
      <el-table-column prop="inspectionQuantity" label="检验数量" align="right" />
      <el-table-column prop="passQuantity" label="合格数量" align="right">
        <template #default="scope">
          <span class="pass-quantity">{{ scope.row.passQuantity }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="failQuantity" label="不合格数量" align="right">
        <template #default="scope">
          <span class="fail-quantity" v-if="scope.row.failQuantity > 0">{{ scope.row.failQuantity }}</span>
          <span v-else>0</span>
        </template>
      </el-table-column>
      <el-table-column prop="passRate" label="合格率" align="right">
        <template #default="scope">
          <span :class="getPassRateClass(scope.row.passRate)">{{ scope.row.passRate }}%</span>
        </template>
      </el-table-column>
      <el-table-column prop="result" label="检验结果">
        <template #default="scope">
          <el-tag :type="getResultType(scope.row.result)">{{ getResultText(scope.row.result) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="inspector" label="检验员" />
      <el-table-column prop="inspectionTime" label="检验时间" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">查看详情</el-button>
          <el-button type="warning" link @click="handleEdit(scope.row)" v-if="scope.row.result === 'PENDING'">
            编辑
          </el-button>
          <el-button type="success" link @click="handleReport(scope.row)">检验报告</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="qualityInspection">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { CirclePlus, Download } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 查询表单
const searchForm = reactive({
  inspectionNo: "",
  productName: "",
  inspectionType: "",
  result: "",
  dateRange: []
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取检验类型标签类型
const getTypeTagType = (type: string) => {
  const typeMap: Record<string, string> = {
    FIRST_PIECE: "primary",
    PROCESS: "info",
    FINAL: "warning",
    OUTGOING: "success"
  };
  return typeMap[type] || "info";
};

// 获取检验类型文本
const getTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    FIRST_PIECE: "首件检验",
    PROCESS: "过程检验",
    FINAL: "最终检验",
    OUTGOING: "出货检验"
  };
  return typeMap[type] || type;
};

// 获取结果类型
const getResultType = (result: string) => {
  const resultMap: Record<string, string> = {
    PASS: "success",
    FAIL: "danger",
    PENDING: "warning"
  };
  return resultMap[result] || "info";
};

// 获取结果文本
const getResultText = (result: string) => {
  const resultMap: Record<string, string> = {
    PASS: "合格",
    FAIL: "不合格",
    PENDING: "待检验"
  };
  return resultMap[result] || result;
};

// 获取合格率样式
const getPassRateClass = (passRate: number) => {
  if (passRate >= 95) return "pass-rate-excellent";
  if (passRate >= 90) return "pass-rate-good";
  if (passRate >= 80) return "pass-rate-normal";
  return "pass-rate-poor";
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // 模拟数据
    const mockData = [
      {
        id: 1,
        inspectionNo: "QC202401150001",
        inspectionType: "FINAL",
        productCode: "FP001",
        productName: "智能手机X1",
        batchNo: "B20240115001",
        inspectionQuantity: 100,
        passQuantity: 98,
        failQuantity: 2,
        passRate: 98.0,
        result: "PASS",
        inspector: "张三",
        inspectionTime: "2024-01-15 14:30:00"
      },
      {
        id: 2,
        inspectionNo: "QC202401150002",
        inspectionType: "PROCESS",
        productCode: "FP002",
        productName: "工业机器人A1",
        batchNo: "B20240115002",
        inspectionQuantity: 10,
        passQuantity: 8,
        failQuantity: 2,
        passRate: 80.0,
        result: "FAIL",
        inspector: "李四",
        inspectionTime: "2024-01-15 16:15:00"
      },
      {
        id: 3,
        inspectionNo: "QC202401150003",
        inspectionType: "FIRST_PIECE",
        productCode: "FP003",
        productName: "精密仪器C1",
        batchNo: "B20240115003",
        inspectionQuantity: 5,
        passQuantity: 0,
        failQuantity: 0,
        passRate: 0,
        result: "PENDING",
        inspector: "王五",
        inspectionTime: "2024-01-15 17:00:00"
      }
    ];
    
    tableData.value = mockData;
    pagination.total = mockData.length;
  } catch (error) {
    console.error("获取质检记录失败:", error);
    ElMessage.error("获取质检记录失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 新增检验
const handleAdd = () => {
  ElMessage.info("新增质检记录功能开发中");
};

// 导出
const handleExport = () => {
  ElMessage.info("导出功能开发中");
};

// 查看详情
const handleView = (row: any) => {
  ElMessage.info(`查看检验单 ${row.inspectionNo} 详情`);
};

// 编辑检验
const handleEdit = (row: any) => {
  ElMessage.info(`编辑检验单 ${row.inspectionNo}`);
};

// 检验报告
const handleReport = (row: any) => {
  ElMessage.info(`生成检验单 ${row.inspectionNo} 报告`);
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

.pass-quantity {
  color: #67c23a;
  font-weight: bold;
}

.fail-quantity {
  color: #f56c6c;
  font-weight: bold;
}

.pass-rate-excellent {
  color: #67c23a;
  font-weight: bold;
}

.pass-rate-good {
  color: #409eff;
  font-weight: bold;
}

.pass-rate-normal {
  color: #e6a23c;
  font-weight: bold;
}

.pass-rate-poor {
  color: #f56c6c;
  font-weight: bold;
}
</style>